package DataBase

import (
	"PaySalary/Model"
)

var theInstance = &PayrollDatabase{map[int]*Model.Employee{}, map[int]int{}}

type PayrollDatabase struct {
	itsEmployees map[int]*Model.Employee
	itsUnionMembers map[int]int
}

func (p *PayrollDatabase)clear() {
	p.itsEmployees = map[int]*Model.Employee{}
	p.itsUnionMembers = map[int]int{}
}

func (p *PayrollDatabase)GetEmployee(empid int) *Model.Employee {
	if _, ok := p.itsEmployees[empid]; ok {
		return p.itsEmployees[empid]
	}
	return &Model.Employee{}
}

func (p *PayrollDatabase)AddEmployee(empid int, e *Model.Employee)  {
	p.itsEmployees[empid] = e
}

func (p *PayrollDatabase) DeleteEmployee(empid int)  {
	delete(p.itsEmployees,empid)
}

func (p *PayrollDatabase) AddUnionMember(memberid int, e Model.Employee)  {
	employeeId := e.GetEmployeeId()
	p.itsUnionMembers[memberid] = employeeId
}

func (p *PayrollDatabase)GetUnionMember(memberId int) *Model.Employee {
	if uk, ok :=  p.itsUnionMembers[memberId]; ok {
		return p.itsEmployees[uk]
	}

	return &Model.Employee{}
}

func (p *PayrollDatabase)RemoveUnionMember(memberId int)  {
	delete(p.itsUnionMembers,memberId)
}

func (p *PayrollDatabase)GetAllEmployeeIds() []int {
	 var EmployeeIds []int

	for _, value := range p.itsUnionMembers {
		EmployeeIds = append(EmployeeIds, value)
	}
	return EmployeeIds
}

